package com.codename1.samples;


import com.codename1.components.SpanLabel;
import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.ui.Component;
import com.codename1.ui.Graphics;
import com.codename1.ui.LinearGradientPaint;
import com.codename1.ui.MultipleGradientPaint;
import com.codename1.ui.MultipleGradientPaint.CycleMethod;
import static com.codename1.ui.MultipleGradientPaint.CycleMethod.NO_CYCLE;
import static com.codename1.ui.MultipleGradientPaint.CycleMethod.REFLECT;
import static com.codename1.ui.MultipleGradientPaint.CycleMethod.REPEAT;
import com.codename1.ui.geom.GeneralPath;
import com.codename1.ui.geom.Rectangle;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.spinner.Picker;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class LinearGradientSample {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    CycleMethod cycleMethod = NO_CYCLE;
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        Form hi = new Form("Hi World", new BorderLayout());
        Picker picker = new Picker();
        picker.setStrings("No cycle", "Repeat", "Reflect");
        picker.addActionListener(e->{
            if ("No cycle".equals(picker.getValue())) {
                cycleMethod = NO_CYCLE;
            } else if ("Repeat".equals(picker.getValue())) {
                cycleMethod = REPEAT;
            } else if ("Reflect".equals(picker.getValue())) {
                cycleMethod = REFLECT;
            }
            hi.repaint();
        });
        hi.add(BorderLayout.NORTH, BoxLayout.encloseY(new SpanLabel("Drag pointer below to generate gradients"), new Label("Gradient Cycle Type:"), picker));
        hi.add(BorderLayout.CENTER, new MyComponent());
        hi.show();
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

    public class MyComponent extends Component {
        
        boolean dragged;
        int startX, startY, endX, endY;
        
        private int startX() {
            if (dragged) return startX;
            return getX();
        }
        
        private int endX() {
            if (dragged) {
                return endX;
            }
            return getX() + getWidth();
        }
        
        private int startY() {
            if (dragged) return startY;
            return getY();
        }
        
        private int endY() {
            if (dragged) return endY;
            return getY() + getHeight();
        }

        @Override
        public void paint(Graphics g) {
            /*
            public LinearGradientPaint(float startX, float startY, float endX, float endY, float[] fractions, int[] colors, MultipleGradientPaint.CycleMethod cycleMethod, MultipleGradientPaint.ColorSpaceType colorSpace, Transform gradientTransform) {
            */
            g.setColor(new LinearGradientPaint(startX(), startY(), endX(), endY(), new float[]{0f, 1f}, new int[]{0xff0000, 0x0000ff}, cycleMethod, MultipleGradientPaint.ColorSpaceType.SRGB, null));
            GeneralPath p = new GeneralPath();
            p.setRect(new Rectangle(getX(), getY(), getWidth(), getHeight()), null);
            g.fillShape(p);
        }

        @Override
        public void pointerPressed(int x, int y) {
            super.pointerPressed(x, y);
            startX = x-getParent().getAbsoluteX();
            startY = y-getParent().getAbsoluteY();
            endX = getX() + getWidth();
            endY = getY() + getHeight();
        }

        @Override
        public void pointerDragged(int x, int y) {
            super.pointerDragged(x, y); 
            dragged = true;
            endX = x-getParent().getAbsoluteX();
            endY = y-getParent().getAbsoluteY();
            repaint();
            
        }
        
        
        
        
        
    }
    
}
